package com.hyg.lib_music.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.EventBusMessage;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_music.R;
import com.hyg.lib_music.modle.ClockData;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * @author: Administrator
 * @date: 2021/4/25
 */
public class ClockSetttingsAdapter extends RecyclerView.Adapter<ClockSetttingsAdapter.ViewHolder> {

    Context context;
    ArrayList<ClockData> list;

    public ClockSetttingsAdapter(Context context, ArrayList<ClockData> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_clock_settings, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tv_name.setText(list.get(position).getName());
        holder.imageView.setVisibility(list.get(position).isSelect() ? View.VISIBLE : View.GONE);
        if (position == list.size() - 1) {
            holder.divider.setVisibility(View.GONE);
        }
        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (ClockData data : list) {
                    data.setSelect(false);
                }
                list.get(position).setSelect(true);
                notifyDataSetChanged();
                EventBus.getDefault().post(new EventBusMessage("time" + position));
                UserSPUtils.setMusicSetting(context, System.currentTimeMillis(), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView imageView;
        RelativeLayout rl_item;
        View divider;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            imageView = itemView.findViewById(R.id.iv_select);
            rl_item = itemView.findViewById(R.id.rl_item);
            divider = itemView.findViewById(R.id.vw_divider);
        }
    }
}
